<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
        当前浏览器不支持Canvas，请更换浏览器
    </canvas>
    <script>
        window.onload = function () {
            var canvas = document.getElementById("canvas");

            canvas.width = 800;
            canvas.height = 800;

            var context = canvas.getContext("2d");

            context.save();
            context.fillStyle = 'red';
            context.translate(100, 100);
            context.fillRect(0, 0, 400, 400);
            context.restore();  //与save成对出现
            // context.translate(-100, -100);

            context.save();
            context.fillStyle = 'green';
            context.translate(300, 300);
            context.fillRect(0, 0, 400, 400);
            context.restore();  //与save成对出现
            // context.translate(-300, -300);
        }
    </script>
</body>
</html>